package DAO;

import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;

import com.mysql.jdbc.PreparedStatement;

import Main.DBMain;

public class AddStudent extends JFrame {
	static int colNum = 0;
	static String[] col = new String[15];
	JLabel lab_No, lab_Name, lab_Class, lab_Pwd;
	JTextField tf_No, tf_Name, tf_Class, tf_Pwd;

	String getStuNo = "";
	String sql = "";
	boolean stuNoIsExist = false;

	public AddStudent() {
		setTitle("添加学生");
		JPanel pan_No = new JPanel();
		JPanel pan_Name = new JPanel();
		JPanel pan_Class = new JPanel();
		JPanel pan_Pwd = new JPanel();
		setSize(300, 500);
		colNum = getCol();
		setLocationRelativeTo(null);

		lab_No = new JLabel("学号");
		tf_No = new JTextField(15);
		tf_No.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (Character.isDigit(c)) {
					return;
				} else {
					//e.consume();// 清除输入的非数字内容
				}
			}
		});
		pan_No.add(lab_No);
		pan_No.add(tf_No);





		lab_Name = new JLabel("姓名");
		tf_Name = new JTextField(15);
		pan_Name.add(lab_Name);
		pan_Name.add(tf_Name);

		lab_Class = new JLabel("班级");
		tf_Class = new JTextField(15);
		tf_Class.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (Character.isDigit(c)) {
					return;
				} else {
					e.consume();// 清除输入的非数字内容
				}
			}
		});

		pan_Class.add(lab_Class);
		pan_Class.add(tf_Class);






		lab_Pwd = new JLabel("密码");
		tf_Pwd = new JTextField(15);
		tf_Pwd.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
					isExist();// 判断学号是否已经存在数据库中
				if (!stuNoIsExist) {
					addStu(); // 数据库中不存在输入的学号，进行添加操作
					tf_No.setText("");
					tf_Name.setText("");
					tf_Class.setText("");
					tf_Pwd.setText("");
				} else {
					JOptionPane.showMessageDialog(null, "该学号已经存在！");
				}

			}
		});
		pan_Pwd.add(lab_Pwd);
		pan_Pwd.add(tf_Pwd);






		JPanel pan_Btn = new JPanel();
		JButton btn_OK = new JButton("确定");
		// 添加按钮监听，点击进行添加学生
		btn_OK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				isExist();// 判断学号是否已经存在数据库中
				if (!stuNoIsExist) {
					addStu();// 数据库中不存在输入的学号，进行添加操作
				} else {
					JOptionPane.showMessageDialog(null, "该学号已经存在！");
				}
			}
		});
		JButton btn_Cancle = new JButton("取消");

		// 点击取消则隐藏该窗口
		btn_Cancle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		pan_Btn.add(btn_OK);
		pan_Btn.add(btn_Cancle);



		getContentPane().setLayout(new GridLayout(5, 1));
		getContentPane().add(pan_No);
		getContentPane().add(pan_Name);
		getContentPane().add(pan_Class);
		getContentPane().add(pan_Pwd);
		getContentPane().add(pan_Btn);

		pack();
		setVisible(true);
	}



	public static int getCol() {
		int num = 0;
		Connection conn = null;
		java.sql.PreparedStatement pstmt = null;
		ResultSet rs;
		try {
			conn = DBMain.getConnection();
			String sql = "select * from stu_info";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery(sql);
			ResultSetMetaData colName = rs.getMetaData();
			num = colName.getColumnCount();
			if (rs.next()) {
				int i, j;
				for (i = 0, j = 1; i < colNum && j <= colNum; i++, j++)
					col[i] = colName.getColumnName(j);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return num;
	}


	public void isExist() {
		getStuNo = tf_No.getText().trim();
		java.sql.PreparedStatement pstmt = null;
		Connection conn1 = null;
		ResultSet rs = null;
		if (!("".equals(getStuNo))) {
			// 学号不为空，进入数据库查询该学号是否已经存在
			try {
				conn1 = DBMain.getConnection();
				sql = "select * from stu_info where stuNo=?";
				pstmt = conn1.prepareStatement(sql);
				pstmt.setString(1, getStuNo);
				rs = pstmt.executeQuery();
				if (rs.next()) {
					stuNoIsExist = true;
				} else {
					stuNoIsExist = false;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "学号为空，请重新输入！");
		}
	}


	private void addStu() {
		Connection conn2 = null;
		java.sql.PreparedStatement pstmt = null;
		// 获取各文本框中输入的数据
		String name = tf_Name.getText().trim();
		String clas = tf_Class.getText().trim();
		String pwd = tf_Pwd.getText().trim();

		if (!("".equals(name)) || !("".equals(clas)) || !("".equals(pwd))) {

			try {
				conn2 = DBMain.getConnection();
				sql = "insert into stu_info(stuNo,stuName,classNo,stuPwd) values (?,?,?,?)";// 添加语句
				pstmt = conn2.prepareStatement(sql);
				pstmt.setString(1, getStuNo);
				pstmt.setString(2, name);
				pstmt.setString(3, clas);
				pstmt.setString(4, pwd);
				pstmt.executeUpdate();// 添加并更新信息
				JOptionPane.showMessageDialog(null, "添加学生成功！");
				setVisible(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "信息不完整，请重新输入！");
		}

	}
}
